package com.guang.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guang.dao.CheckItemDao;
import com.guang.entity.PageResult;
import com.guang.pojo.CheckItem;
import com.guang.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("checkItemService")
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;
  
    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //删除
    @Override
    public void delete(Integer id) throws RuntimeException{
        //查询当前检查项是否和检查组关联
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count > 0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前检查项被检查组引用，不能删除");
        }
        checkItemDao.deleteById(id);
    }

    @Override
    public PageResult findAllCheckItemFroCondition(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page =  checkItemDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }

    // 主键查询
    @Override
    public CheckItem findById(Integer id) {
        CheckItem checkItem = checkItemDao.findCheckItemById(id);
        return checkItem;
    }

    // 编辑保存
    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

}
